package com.mio.scalademo

object BeerChange {
    def main(args: Array[String]): Unit = {
    //每瓶啤酒2元
    val buyNum = buy(0,100,2)
    //3个空酒瓶
    val botChangeNum = changeBottle(0,buyNum,3)
    //5个瓶盖
    val capChangeNum = changeCap(0,buyNum+botChangeNum,5,0)
    printf("100元最多可喝 %s 瓶啤酒 ", buyNum)
    println("")
//    printf("空酒瓶最多可喝 %s ", botChangeNum)
    printf("瓶盖加瓶子最多可喝 %s 瓶啤酒", botChangeNum +capChangeNum)
  }
  def buy(i: Int, momney: Int, num: Int): Int =  { 
    if(momney >= num)
      buy(i +1,momney - num,num)
    else
      i
  }
  //i 可以兑换的数量，bottleCapNum 剩余瓶子或者瓶盖的数量 num兑换需要的数量
   def changeBottle(i: Int, bottleNum: Int, num: Int): Int =  {
    if(bottleNum >= num){
      //瓶子的数量减去兑换的数量，同时+1是新增的一瓶的瓶子
      var inum = i +1
      var bcnum = bottleNum - num +1
      changeBottle(inum,bcnum,num)
    }else{
      i
    }
  }
  //i 可以兑换的数量，bottleCapNum 剩余瓶子或者瓶盖的数量 num兑换需要的数量
   def changeCap(i: Int, capNum: Int, num: Int,botChangeNum: Int): Int =  {
     var inum = i +1
     //瓶盖的数量减去兑换的数量，同时+1是新增的一瓶的瓶盖
     var cnum = capNum - num +1
    
     //可以用3个空酒瓶再兑换一瓶啤酒
     var bcnum = botChangeNum
     if(botChangeNum  == 3){
       inum = i +1
       bcnum = 0
       cnum = cnum +1
     }
     bcnum = botChangeNum +1
      
    if(capNum >= num){
      changeCap(inum,cnum,num,bcnum)
    }else{
      i
    }
  }
}